package com.suben.serviceucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suben.common.bean.R;
import com.suben.serviceucenter.entity.GvUser;
import com.suben.serviceucenter.mapper.GvUserMapper;
import com.suben.serviceucenter.service.GvUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Suben
 * @since 2020-11-22
 */
@Service
public class GvUserServiceImpl extends ServiceImpl<GvUserMapper, GvUser> implements GvUserService {

    @Override
    public R login(String mobile, String pwd) {
        GvUser user = findUser(mobile,pwd);
        if (user == null) return R.error().message("用户名或者密码错误~");
        return R.ok().message("登录成功~").data("data",user);
    }

    @Override
    public R register(GvUser user) {
        GvUser isExistUser = findUser(user.getMobile(),user.getPwd());
        if (isExistUser != null) return R.error().message("该用户已经存在~");
        boolean isSaveSuccess = save(user);
        if (!isSaveSuccess) return R.error().message("注册失败，请联系管理员~");
        return R.ok().message("恭喜您注册成功~");
    }

    private GvUser findUser(String mobile,String pwd){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile",mobile);
        queryWrapper.eq("pwd",pwd);
        return getOne(queryWrapper);
    }
}
